package com.mlm.mlmapp.adapter.view;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.androidquery.AQuery;
import com.flyco.dialog.widget.base.BaseDialog;
import com.mlm.mlmapp.R;
import com.mlm.mlmapp.activity.layoutmanager.SyLayoutManager;
import com.mlm.mlmapp.adapter.view.listener.DialogListener;
import com.mlm.mlmapp.adapter.view.listener.DresserClickListener;
import com.mlm.mlmapp.cons.HTTPCons;
import com.mlm.mlmapp.utils.CommonUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CityFilterDialog extends BaseDialog implements HTTPCons {
    private Context mContext;
    public static final int PAYMENT_TYPE_ALIPAY = 1;
    public static final int PAYMENT_TYPE_WECHAT = 2;
    List<JSONObject> citys = new ArrayList<>();
    List<JSONObject> studios = new ArrayList<>();
    Map<String, JSONArray> studiosMap = new HashMap<String, JSONArray>();
    RecyclerView rv_city;
    RecyclerView rv_studio;
    AQuery aq;
    DialogListener l;
    JSONArray array;
    String key;
    String studoValue;
    public CityFilterDialog(Context context, DialogListener l, JSONArray array, String key,String studoValue) {
        super(context);
        // TODO Auto-generated constructor stub
        this.mContext = context;
        this.l = l;
        aq = new AQuery(context);
        this.array = array;

        this.key = key;
        this.studoValue = studoValue;
        try {
            JSONArray a=new JSONArray();

            JSONObject allObj=new JSONObject();
            allObj.put("area", "全部区域");
            citys.add(allObj);

          for (int i = 0; i < array.length(); i++) {
                JSONObject cityObj = array.getJSONObject(i);
                citys.add(cityObj);
                JSONArray studioArr = cityObj.getJSONArray("studioLst");
                studiosMap.put(cityObj.getString("area"), studioArr);
                for(int j=0;j<studioArr.length();j++){
                    a.put(studioArr.getJSONObject(j));
                }

            }
            studiosMap.put("全部区域",a);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public View onCreateView() {
        // TODO Auto-generated method stub

        View inflate = View.inflate(context, R.layout.city_filter_dialog, null);

        rv_city = (RecyclerView) inflate.findViewById(R.id.rv_city);
        rv_studio = (RecyclerView) inflate.findViewById(R.id.rv_studio);
        RecyclerView.LayoutManager cityManager = new SyLayoutManager(context);
        rv_city.setLayoutManager(cityManager);
        RecyclerView.LayoutManager studioManager = new SyLayoutManager(context);
        rv_studio.setLayoutManager(studioManager);
        rv_city.setAdapter(new CityAdapter(context, citys));
        rv_studio.setAdapter(new StudioAdapter(context, studios));
        if (key == null || "".equals(key) || !studiosMap.containsKey(key)) {
            key="全部区域";
        }
        refresh(key);
        return inflate;
    }
    public void refresh(String key){
        //set row selected
        //setDataset and notify
        try {
            JSONArray array = studiosMap.get(key);
            studios.clear();
            for (int i = 0; i < array.length(); i++) {
                studios.add(array.getJSONObject(i));
            }
            rv_studio.getAdapter().notifyDataSetChanged();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    @Override
    public boolean setUiBeforShow() {
        // TODO Auto-generated method stub
        return false;
    }

    class CityAdapter extends
            RecyclerView.Adapter<CityAdapter.CityHolder> {
        private Context mContext;
        private List<JSONObject> array;

        public CityAdapter(Context con, List<JSONObject> array) {
            this.mContext = con;
            this.array = array;

        }

        @Override
        public int getItemCount() {
            return array.size();
        }

        @Override
        public CityHolder onCreateViewHolder(ViewGroup parent, int arg1) {
            // TODO Auto-generated method stub
            CityHolder holder;
            holder = new CityHolder(LayoutInflater.from(
                    context).inflate(
                    R.layout.city_list_item, parent, false));
            return holder;

        }

        @Override
        public void onBindViewHolder(CityHolder holder, int position) {
            // TODO Auto-generated method stub
            JSONObject obj = array.get(position);
            CityHolder hHolder = holder;
            final String keyFlag=CommonUtils.getJsonString(obj, "area");
            hHolder.value.setText(keyFlag);
            if(keyFlag.equals(key)){
                hHolder.itemView.setBackgroundColor(Color.RED);
            }
            hHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    refresh(keyFlag);
                    view.setBackgroundColor(Color.RED);
                }
            });

        }


        public class CityHolder extends RecyclerView.ViewHolder {
            TextView value;


            public CityHolder(View itemView) {
                super(itemView);
                this.value = (TextView) itemView.findViewById(R.id.tv_name);

            }
        }

    }

    class StudioAdapter extends
            RecyclerView.Adapter<StudioAdapter.StudioHolder> {
        private Context mContext;
        private List<JSONObject> array;

        public StudioAdapter(Context con, List<JSONObject> array) {
            this.mContext = con;
            this.array = array;

        }

        @Override
        public int getItemCount() {
            return array.size();
        }

        @Override
        public StudioHolder onCreateViewHolder(ViewGroup parent, int arg1) {
            // TODO Auto-generated method stub
            StudioHolder holder;
            holder = new StudioHolder(LayoutInflater.from(
                    context).inflate(
                    R.layout.city_list_item, parent, false));
            return holder;

        }

        @Override
        public void onBindViewHolder(StudioHolder holder, int position) {
            // TODO Auto-generated method stub
            JSONObject obj = array.get(position);
            StudioHolder hHolder = holder;
            final String id=CommonUtils.getJsonString(obj, "id");
            final String name=CommonUtils.getJsonString(obj, "names");
            hHolder.value.setText(name);
            if(id.equals(studoValue)){
                hHolder.itemView.setBackgroundColor(Color.RED);
            }
            hHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    JSONObject obj=new JSONObject();

                    try {
                        obj.put("area", key);
                        obj.put("studio",name);
                        obj.put("studioid",id);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    l.onApply(obj,null);
                    view.setBackgroundColor(Color.RED);
                }
            });

        }


        public class StudioHolder extends RecyclerView.ViewHolder {
            TextView value;


            public StudioHolder(View itemView) {
                super(itemView);
                this.value = (TextView) itemView.findViewById(R.id.tv_name);

            }
        }

    }
}
